package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.TypeDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.Type;
import com.bestv.search.common.model.uid.UID;
import com.bestv.search.common.service.TypeManager;

import java.util.List;

/**
 * @author Michael Wang
 */

// $Id$
public class TypeManagerImpl implements TypeManager {

    private TypeDao typeDao;

    private List<Type> cachedTypeList;

    public TypeDao getTypeDao() {
        return typeDao;
    }

    public void setTypeDao(TypeDao typeDao) {
        this.typeDao = typeDao;
    }

    public List<Type> getAllType(DataSourceKey key) {
        if (cachedTypeList == null) {
            cachedTypeList = typeDao.getAll();
        }
        return cachedTypeList;
    }

    public void deleteTypeById(DataSourceKey key, Long id) {
        typeDao.remove(id);
    }

    public Type getTypeById(DataSourceKey key, Long id) {
        return typeDao.getTypeById(id);
    }

    public Type getTypeByName(DataSourceKey key, String name) {
        return typeDao.getTypeByName(name);
    }

    public List<Type> getTypes(DataSourceKey key) {
        return typeDao.getTypes();
    }

    public Type saveType(DataSourceKey key, Type type) {
        if (type.getId() == 0L) {
            type.setId(new Long(UID.getUID(type)));
        }
        return typeDao.saveOrUpdate(type);
    }
}
